package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.CommonService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoanService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLai;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLaiService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienHocPhiBienLaiUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private String soBienLai; 
    public void setSoBienLai(String soBienLai) {
       this.soBienLai = soBienLai;
 }

    public String getSoBienLai() {
       return soBienLai;
 }

    private String ngayLap; 
    public void setNgayLap(String ngayLap) {
       this.ngayLap = ngayLap;
 }

    public String getNgayLap() {
       return ngayLap;
 }

    private double soTien; 
    public void setSoTien(double soTien) {
       this.soTien = soTien;
 }

    public double getSoTien() {
       return soTien;
 }

    private String dienGiai; 
    public void setDienGiai(String dienGiai) {
       this.dienGiai = dienGiai;
 }

    public String getDienGiai() {
       return dienGiai;
 }

    private TblQLDTTXAHocVienHocPhiBienLai tblQLDTTXAHocVienHocPhiBienLai; 
    public void setTblQLDTTXAHocVienHocPhiBienLai (TblQLDTTXAHocVienHocPhiBienLai tblQLDTTXAHocVienHocPhiBienLai) {
        this.tblQLDTTXAHocVienHocPhiBienLai = tblQLDTTXAHocVienHocPhiBienLai;
 }

    private Collection<TblQLDTTXAHocVienHocPhiBienLai> tblQLDTTXAHocVienHocPhiBienLais; 
    public void setTblQLDTTXAHocVienHocPhiBienLais(Collection<TblQLDTTXAHocVienHocPhiBienLai> tblQLDTTXAHocVienHocPhiBienLais) {
        this.tblQLDTTXAHocVienHocPhiBienLais = tblQLDTTXAHocVienHocPhiBienLais; 
      } 

    private TblQLDTTXAHocVienHocPhiBienLaiService tblQLDTTXAHocVienHocPhiBienLaiService; 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private String tblQLDTTXAHocVienBienLaiMonHocStr; 
    public void setTblQLDTTXAHocVienBienLaiMonHocStr (String tblQLDTTXAHocVienBienLaiMonHocStr) 
 {     this.tblQLDTTXAHocVienBienLaiMonHocStr = tblQLDTTXAHocVienBienLaiMonHocStr;
 }

    public String getTblQLDTTXAHocVienBienLaiMonHocStr() {
       return tblQLDTTXAHocVienBienLaiMonHocStr;
 }

    private TblQLDTTXAHocVienBienLaiMonHocService tblQLDTTXAHocVienBienLaiMonHocService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 
    
    private TblQLDTTXAHocVienTaiKhoanService tblQLDTTXAHocVienTaiKhoanService;
    
    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;

    private TblQLDTTXAHocVienTaiKhoan tblQLDTTXAHocVienTaiKhoan;
    
	private int preUpdate(Message msg, TblQLDTTXAHocVienHocPhiBienLai obj ){ 
		
		tblQLDTTXAHocVienTaiKhoan.setSoDuTaiKhoan(tblQLDTTXAHocVienTaiKhoan.getSoDuTaiKhoan() - obj.getSoTien());
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXAHocVienHocPhiBienLai obj ){ 
		
		try {
			tblQLDTTXAHocVienTaiKhoan.setSoDuTaiKhoan(tblQLDTTXAHocVienTaiKhoan.getSoDuTaiKhoan() + obj.getSoTien());
			tblQLDTTXAHocVienTaiKhoanService.updatetblQLDTTXAHocVienTaiKhoan(tblQLDTTXAHocVienTaiKhoan);
		
		} catch (Exception e) {
			 msg.setPostMessage_b(false); 
		     msg.setPostMessage_s("Không thể cập nhật tài khoản học viên!"); 
		     return 0; 
		}
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_HocPhi_BienLai", "sua")){ 
        return "noprivilege"; 
    }        tblQLDTTXAHocVienHocPhiBienLaiService = (TblQLDTTXAHocVienHocPhiBienLaiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhi_BienLaiClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienBienLaiMonHocService = (TblQLDTTXAHocVienBienLaiMonHocService) context.getBean("tbl_QLDTTXA_HocVien_BienLai_MonHocClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient");
        
        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");

        tblQLDTTXAHocVienTaiKhoanService = (TblQLDTTXAHocVienTaiKhoanService) context.getBean("tbl_QLDTTXA_HocVien_TaiKhoanClient");
	    

        CommonService commonService = (CommonService) context.getBean("commonClient");
        
        msg = new Message(); 
        tblQLDTTXAHocVienHocPhiBienLai = tblQLDTTXAHocVienHocPhiBienLaiService.gettblQLDTTXAHocVienHocPhiBienLai(new Long(id)); 
        
        
        //get tai khoan hoc vien
        List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
        {
	        ConditionReport con = new ConditionReport();
	  		con.setCol("hocVienId.id");
	  		con.setOperator("eq");
	  		con.setValue1(new Long(hocVienId));
	  		cons.add(con);
			List<TblQLDTTXAHocVienTaiKhoan> tblQLDTTXAHocVienTaiKhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
			if (tblQLDTTXAHocVienTaiKhoans != null && tblQLDTTXAHocVienTaiKhoans.size() > 0){
				tblQLDTTXAHocVienTaiKhoan = tblQLDTTXAHocVienTaiKhoans.get(0);
			}
        }
		
		
        preUpdate(msg,tblQLDTTXAHocVienHocPhiBienLai); 
        if (msg.isPreMessage_b()) 
        { 
        
//        tblQLDTTXAHocVienHocPhiBienLai.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
//        tblQLDTTXAHocVienHocPhiBienLai.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId))); 
//        tblQLDTTXAHocVienHocPhiBienLai.setSoBienLai(soBienLai); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayLap); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        tblQLDTTXAHocVienHocPhiBienLai.setNgayLap(d); 
        tblQLDTTXAHocVienHocPhiBienLai.setSoTien(soTien);
         
        DecimalFormat myFormatter = new DecimalFormat("###,###.##");        
        Date today = new Date();
        if(dienGiai == null)
        	dienGiai = "";
        dienGiai += "<br/>-------------------------------- ";
        dienGiai += "<br/><i>Updated at " + (new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(today)) + " by " + taiKhoanDangNhap.getUsername() + "(" + taiKhoanDangNhap.getCanBoId().getTen() + ")</i> ";
        dienGiai += "<br/>Số tiền thu: " + myFormatter.format(soTien);
        
        tblQLDTTXAHocVienHocPhiBienLai.setCanBoId(taiKhoanDangNhap.getCanBoId());
        
    // xoa cac bienLaiMonHoc cu
    cons.clear();
    ConditionReport con = new ConditionReport(); 
    con.setCol("bienLaiId.id"); 
    con.setValue1(tblQLDTTXAHocVienHocPhiBienLai.getId()); 
    con.setOperator("eq"); 
    cons.add(con); 
    List<TblQLDTTXAHocVienBienLaiMonHoc> tblQLDTTXAHocVienBienLaiMonHocList = tblQLDTTXAHocVienBienLaiMonHocService.findtblQLDTTXAHocVienBienLaiMonHocByCondition(cons, 0, 0); 
    if(tblQLDTTXAHocVienBienLaiMonHocList != null && tblQLDTTXAHocVienBienLaiMonHocList.size() >0){ 
         List<String> ids = new ArrayList<String>();  
         for(TblQLDTTXAHocVienBienLaiMonHoc obj0 : tblQLDTTXAHocVienBienLaiMonHocList ){ 
             //Collections.addAll(ids, obj0.getId().toString());
             // set hocVienHocPhiMonHoc tuong ung voi cac bienLaiMonHoc cu ve chua dong hoc phi
        	 //tblQLDTTXAHocVienBienLaiMonHocService.deletetblQLDTTXAHocVienBienLaiMonHoc(obj0.getId());
        	 String delSQL = "DELETE FROM Tbl_QLDTTXA_HocVien_BienLai_MonHoc BLMH WHERE BLMH.id = " + obj0.getId();
        	 commonService.executeQuery(delSQL);
        	 
             cons.clear();
             {            	 
            	ConditionReport con1 = new ConditionReport(); 
        	    con1.setCol("hocVienHocPhiId.hocVienId.id"); 
        	    con1.setValue1(obj0.getBienLaiId().getHocVienId().getId()); 
        	    con1.setOperator("eq"); 
        	    cons.add(con1);
             }
             {            	 
             	ConditionReport con1 = new ConditionReport(); 
         	    con1.setCol("hocVienHocPhiId.hocKyId.id"); 
         	    con1.setValue1(obj0.getBienLaiId().getHocKyId().getId()); 
         	    con1.setOperator("eq"); 
         	    cons.add(con1);
              }
             {            	 
             	ConditionReport con1 = new ConditionReport(); 
         	    con1.setCol("monHocId.id"); 
         	    con1.setValue1(obj0.getMonHocId().getId()); 
         	    con1.setOperator("eq"); 
         	    cons.add(con1);
              }
             List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
             for(TblQLDTTXAHocVienHocPhiMonHoc hpmh : tblQLDTTXAHocVienHocPhiMonHocs){
            	 hpmh.setDaDongHP(false);
            	 tblQLDTTXAHocVienHocPhiMonHocService.updatetblQLDTTXAHocVienHocPhiMonHoc(hpmh);
             }
         } 
         //tblQLDTTXAHocVienBienLaiMonHocService.deleteListtblQLDTTXAHocVienBienLaiMonHoc(ids);  
     }
    
    dienGiai += "<br/>Môn học đóng học phí: ";    
    // them cac bienLaiMonHoc moi
    JSONArray jsonTblQLDTTXAHocVienBienLaiMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienBienLaiMonHocStr); 
    for (int i = 0; i < jsonTblQLDTTXAHocVienBienLaiMonHoc.size(); i++) 
     { 
         JSONObject jsObj = jsonTblQLDTTXAHocVienBienLaiMonHoc.getJSONObject(i); 
         TblQLDTTXAHocVienBienLaiMonHoc obj0 = new TblQLDTTXAHocVienBienLaiMonHoc(); 
       obj0.setBienLaiId(tblQLDTTXAHocVienHocPhiBienLai); 
        obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
       tblQLDTTXAHocVienBienLaiMonHocService.createtblQLDTTXAHocVienBienLaiMonHoc(obj0);
       dienGiai += (obj0.getMonHocId().getTen() != null ? obj0.getMonHocId().getTen() : obj0.getMonHocId().getId().toString()) + ", ";
       
       // set da dong hoc phi cho cac hocVienHocPhiMonHoc tuong ung voi cac bienLaiMonHoc moi 
       cons.clear();
       {            	 
      	ConditionReport con1 = new ConditionReport(); 
  	    con1.setCol("hocVienHocPhiId.hocVienId.id"); 
  	    con1.setValue1(obj0.getBienLaiId().getHocVienId().getId()); 
  	    con1.setOperator("eq"); 
  	    cons.add(con1);
       }
       {            	 
       	ConditionReport con1 = new ConditionReport(); 
   	    con1.setCol("hocVienHocPhiId.hocKyId.id"); 
   	    con1.setValue1(obj0.getBienLaiId().getHocKyId().getId()); 
   	    con1.setOperator("eq"); 
   	    cons.add(con1);
        }
       {            	 
       	ConditionReport con1 = new ConditionReport(); 
   	    con1.setCol("monHocId.id"); 
   	    con1.setValue1(obj0.getMonHocId().getId()); 
   	    con1.setOperator("eq"); 
   	    cons.add(con1);
        }
       List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
       for(TblQLDTTXAHocVienHocPhiMonHoc hpmh : tblQLDTTXAHocVienHocPhiMonHocs){
      	 hpmh.setDaDongHP(true);
      	 tblQLDTTXAHocVienHocPhiMonHocService.updatetblQLDTTXAHocVienHocPhiMonHoc(hpmh);
       }
   } 
    
    tblQLDTTXAHocVienHocPhiBienLai.setDienGiai(dienGiai);
  
        tblQLDTTXAHocVienHocPhiBienLaiService.updatetblQLDTTXAHocVienHocPhiBienLai(tblQLDTTXAHocVienHocPhiBienLai); 
        postUpdate(msg, tblQLDTTXAHocVienHocPhiBienLai); 
        if (msg.isPostMessage_b() == false) 
        { 
            return Action.ERROR; 
        } 
    }else{ 
        return Action.ERROR; 
    } 
    
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_HocVien_HocPhi_BienLai Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
